package com.server.shop;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;

import com.config.AppReturn;
import com.config.ConstDefine;
import com.config.ConstService;
import com.config.PassInter;
import com.server.user.AppUserBaseCtrl;
import com.server.user.UserDAO;
import com.server.userCenter.UserCenterDAO;
import com.server.userCenter.UserCenterService;
import com.server.wx.WxUtil;
import com.util.StrKit;

/**
 * 不需要过滤用户登录得请求接口
 * @author dell
 *
 */
@Controller
@RequestMapping("/appUser/shopOpen")
public class ShopOpenCtrl extends AppUserBaseCtrl{
	
	@Autowired
	ShopService shopService;
	@Autowired
	ConstService constService;
	@Autowired
	ShopDao shopDao;
	@Autowired
	UserCenterService ucService;
	@Autowired
	UserCenterDAO userCenterDao;
	@Autowired
	WxUtil wxUtil;
	
	//跳转到测试页面
	@RequestMapping("/goTest")
	@PassInter
	public ModelAndView goTest()
	{
		ModelAndView mav = new ModelAndView("test/test");
		return mav;
	}
	
	//首页
	@RequestMapping("/index")
	@PassInter
	public ModelAndView index()
	{
		ModelAndView mav = new ModelAndView("shop/index");
		//广告
		List<Map<String, Object>> banner = shopService.getBanner(null);
		//推荐商品
//		List<Map<String, Object>> goods = shopService.getGoodsList(null, null, null,"1");
		//消息
		List<Map<String, Object>> notice = shopService.getNotice("1");
		
		mav.addObject("banner", banner);
//		mav.addObject("goods", goods);
		mav.addObject("notice", notice);
		return mav;
	}
	
	//跳转到某商品分类
	@RequestMapping("/goGoodsClass/{classid}")
	public ModelAndView goGoodsClass(@PathVariable(name="classid")String classid)
	{
		ModelAndView mav = new ModelAndView("shop/classPage");
		if(StrKit.isBlank(classid))classid="1";
		mav.addObject("classid", classid);
		//消息
		List<Map<String, Object>> notice = shopService.getNotice("1");
		mav.addObject("notice", notice);
		//广告
		List<Map<String, Object>> banner = shopService.getBanner(null);
		mav.addObject("banner", banner);		
		return mav;
	}
	
	//跳转到商品详情
	@RequestMapping("/goGoodsDetail")
	public ModelAndView goGoodsDetail(@RequestParam(name="goodsid",required=false)String goodsid)
	{
		//goodsid可能试从session中获取的
		if(StrKit.isBlank(goodsid))
		{
			goodsid=(String) getSession().getAttribute("goodsid");
		}
		//获取分享内容
//		String uri = getReq().getRequestURI();
////		System.out.println("uri:"+uri);
		String url = getReq().getRequestURL().toString()+"?goodsid="+goodsid;
//		System.out.println("url:"+url);
		Map<String, String> sign = wxUtil.sign(url);
		sign.put("appid", ConstDefine.getTxAppid());
		
		
		
		String viewName = "shop/goodsDetail1";
		ModelAndView mav = new ModelAndView();
		Map<String, Object> data = shopService.getGoodsDetail(getLogUser(), goodsid);
		mav.addObject("goods", data.get("goods"));
		mav.addObject("stocks", data.get("stocks"));
		mav.addObject("sign", sign);
		mav.addObject("sid", data.get("sid"));
		
		//判断商品classid
		if(data.get("classid").equals("2"))viewName = "shop/goodsDetail2";
		
		mav.setViewName(viewName);
		return mav; 
	}
	
	//跳转到购物车
	@RequestMapping("/goShopCar")
	@PassInter
	public ModelAndView goShopCar()
	{
		ModelAndView mav = new ModelAndView("shop/shopCar");
		
		return mav; 
	}
	
	//跳转到活动页面
	@RequestMapping("/goActivity")
	@PassInter
	public ModelAndView goActivity()
	{
		ModelAndView mav = new ModelAndView("shop/activity");
		//查询所有活动
		List<Map<String, Object>> data = shopService.getActivity(getLogUser());
		mav.addObject("data", data);
		return mav; 
	}
	
	//跳转到活动页面详情
	@RequestMapping("/goActivityDetail")
	@PassInter
	public ModelAndView goActivityDetail(@RequestParam("id")String id)
	{
		ModelAndView mav = new ModelAndView("shop/activityDetail");
		//查询所有活动
		Map<String, Object> data = shopService.getActivityDetail(getLogUser(), id);
		//跳转到链接
		if(data.get("url")!=null){
			String url = (String)data.get("url");
			if(url.length()>10){
				return new ModelAndView("redirect:"+url);
			}
			
		}
		
		mav.addObject("data", data);
		return mav; 
	}
	
	//跳转到知识页面
	@RequestMapping("/goKnowledge")
	@PassInter
	public ModelAndView goKnowledge()
	{
		ModelAndView mav = new ModelAndView("shop/knowledge");
		//查询所有活动
		List<Map<String, Object>> data = shopService.getKnowledge(getLogUser());
		mav.addObject("data", data);
		return mav; 
	}	
	//跳转到知识页面详情
	@RequestMapping("/goKnowledgeDetail")
	@PassInter
	public ModelAndView goKnowledgeDetail(@RequestParam("id")String id)
	{
		ModelAndView mav = new ModelAndView("shop/knowledgeDetail");
		//查询所有活动
		Map<String, Object> data = shopService.getKnowledgeDetail(getLogUser(), id);
		mav.addObject("data", data);
		return mav; 
	}	
	/**
	 * 查询轮播广告
	 * @param classes
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getBanner")
	@PassInter
	public String getBanner()
	{
		List<Map<String, Object>> list = shopService.getBanner(null);
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	/**
	 * 首页推荐商品
	 * @param classes
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/recommendGoods")
	@PassInter
	public String recommendGoods()
	{
		List<Map<String, Object>> list = shopService.getGoodsList(null, null, null,"1");
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	
	/**
	 * 查询首页消息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getIndexNotice")
	@PassInter
	public String getIndexNotice()
	{
		List<Map<String, Object>> list = shopService.getNotice("1");
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	/**
	 * 查询首页消息
	 * @return
	 */
	@RequestMapping("/goNoticeDetail")
	@PassInter
	public ModelAndView goNoticeDetail(@RequestParam("id")String id)
	{
		ModelAndView mav = new ModelAndView("userCenter/noticeDetail");
		//查询消息详情
		mav.addObject("detail",shopDao.getNoticeDetail(id));
		return mav;
	}
	/**
	 * 查询全部商品分类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getGoodsClasses")
	@PassInter
	public String getGoodsClasses()
	{
		List<Map<String, Object>> list = constService.getGoodsClasses();
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	
	/**
	 * 查询商品列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getGoodsList")
	@PassInter
	public String getGoodsList(@RequestParam(name="name",required=false)String name,@RequestParam(name="classid",required=false)String classid)	
	{
		List<Map<String, Object>> list = shopService.getGoodsList(name, classid, null,null);
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	
	/**
	 * 查询省
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCity")
	@PassInter
	public String getCity(@RequestParam(name="id",required=false)String id,@RequestParam(name="type")String type)	
	{
		List<Map<String, Object>> list = shopService.getCity(id, type);
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}	
	
	
	//跳转到结算清单页面
	@RequestMapping("/goClearList")
	public ModelAndView goClearList(@RequestParam("ids")String ids)
	{
		ModelAndView mav = new ModelAndView("shop/clearList");
		String goodsType = "1";//标识商品类型，1：正常，2：积分兑换
		//根据ID查询购物车商品
		List<Map<String, Object>> goods = shopService.getMyCarByIds(getLogUser(), ids);
		mav.addObject("goods", goods);
		//计算金额
		//封装商品、库存、数量数组
		double total = 0;//总金额
		if(goods!=null&&goods.size()>0)
		{
			String goodsids = "";
			String stockids = "";
			String nums = "";
			for(int i = 0 ;i<goods.size();i++)
			{
				Map<String, Object> goodsDetail = goods.get(i);
				total = total + (Double)goodsDetail.get("price")*(Integer)goodsDetail.get("num");
				if(i!=0)
				{
					goodsids=goodsids+"#";
					stockids=stockids+"#";
					nums=nums+"#";
				}
				goodsids=goodsids+(String) goodsDetail.get("goodsid");
				stockids=stockids+(String) goodsDetail.get("stockid");
				nums=nums+(int) goodsDetail.get("num")+"";
				goodsType=(String)goodsDetail.get("type");
			}
			mav.addObject("goodsids", goodsids);
			mav.addObject("stockids", stockids);
			mav.addObject("nums", nums);
		}
		mav.addObject("total", total);
		
		//返回默认地址
		Map<String, Object> addr = ucService.getDefaultAddrMap(getLogUser());
		mav.addObject("addr", addr);
		//将IDS重新存储
		mav.addObject("ids", ids);
		mav.addObject("goodsType", goodsType);
		
		return mav; 
	}
	
	
	/**
	 * 查询自己的购物车
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMyCar")
	public String getMyCar()
	{
		AppReturn ret = shopService.getMyCar(getLogUser());
		return ret.toString();
	}
	
	/**
	 * 增加到购物车
	 * @param id
	 * @param stockid 库存ID
	 * @param num
	 */
	@ResponseBody
	@RequestMapping("/addCar")
	public String addCar(@RequestParam("id")String id,@RequestParam("stockid")String stockid,@RequestParam(name="num",required=false,defaultValue="1")String num)
	{
		
		AppReturn ret = shopService.addCar(getLogUser(), id, stockid, num);
		
		return ret.toString();
	}
	
	/**
	 * 修改购物车商品数量
	 * @param id 购物车ID
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/upCar")
	public String upCar(@RequestParam("id")String id,@RequestParam(name="num",required=false,defaultValue="1")String num)
	{
		AppReturn ret = shopService.upCar(getLogUser(), id, num);
		return ret.toString();
	}
	/**
	 * 删除购物车中商品
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delCar")
	public String delCar(@RequestParam("id")String id)
	{
		AppReturn ret = shopService.delCar(getLogUser(), id);
		return ret.toString();
	}
	/**
	 * 直接购买商品
	 * @param id
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/buyGoods")
	public String buyGoods(@RequestParam("goodsId")String goodsId,@RequestParam("stockid")String stockid,@RequestParam(name="num",required=false,defaultValue="1")String num,@RequestParam("addrid")String addrid)
	{
		AppReturn ret = shopService.buyGoods(getLogUser(), goodsId, stockid, num, addrid);
		return ret.toString();
	}
	
	/**
	 * 结算购物车中得商品
	 * @param ids
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/buyCarGoods")
	public String buyCarGoods(@RequestParam("ids")String ids,@RequestParam("addrid")String addrid)
	{
		AppReturn ret = shopService.buyCarGoods(getLogUser(), ids, addrid);
		return ret.toString();
	}
	
	/**
	 * 根据商品信息生成订单
	 * @param json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/createList" )
	public String createList(@RequestParam("goodsids")String goodsids,@RequestParam("stockids")String stockids,@RequestParam("nums")String nums,@RequestParam("addrid")String addrid)
	{
		AppReturn ret = shopService.doCreateList(getLogUser(), goodsids, stockids, nums, addrid);
		return ret.toString();
	}
	
	/**
	 * 跳转到支付页面
	 * @param id
	 * @param type 1：普通订单   11：加盟
	 * @return
	 */
	@RequestMapping("/goPayPage")
	public ModelAndView goPayPage(@RequestParam("id")String id,@RequestParam(name="type",required=false,defaultValue="1")String type)
	{
		ModelAndView mav = new ModelAndView("shop/pay");
		mav.addObject("id",id);
		mav.addObject("type",type);
		return mav;
	}
	/**
	 * 积分支付订单
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doPayPoint")
	public String doPayPoint(@RequestParam("listid")String listid)
	{
		AppReturn ret = shopService.doPayPoint(getLogUser(), listid);
		return ret.toString();
	}

	
	/**
	 * 根据商品和收货地址，返回邮费信息——现在都是免费
	 * @param goodsid
	 * @param addrid
	 * @param num
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPostFee" ,produces = "application/json;charset=UTF-8")
	public String getPostFee(HttpServletRequest request)
	{
		String body = getBodyData();
		JSONObject goods = new JSONObject(body);
//		String addrid=goods.getString("addrid");
//		System.out.println("下单地址ID："+addrid);
//		JSONArray jsa = goods.getJSONArray("goods");
//		System.out.println("共有数据："+jsa.length()+"条");
//		
//		
//		JSONObject data = new JSONObject();
//		data.put("fee", "0");
//		data.put("收到下单地址ID", addrid);
		AppReturn ret = shopService.getPostFee(getLogUser(), goods);
		return ret.toString();
	}
}
